﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using House.Server.Dto;
using System.Data;
using House.Server.Common;
namespace House.Server.Repository
{
    /// <summary>
    /// 员工表DAL层
    /// </summary>
    public class System_StaffRepository : GenericBaseRepository<Staff>
    {
        /// <summary>
        ///左查询
        /// </summary>
        /// <returns></returns>
        public List<System_StafforRoleorDepartment> show(int del)
        {
            List<System_StafforRoleorDepartment> list = new List<System_StafforRoleorDepartment>();

            string sql = @"select t_Staff.*,t_Role.* from t_StaffAndRole left join t_Staff on t_StaffAndRole.S_id=t_Staff.S_id left join t_Role on t_StaffAndRole.R_id=t_Role.R_id where S_del=@del";
            using (var cnn = DbConnection)
            {
                using (var cmd = cnn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    var p = cmd.CreateParameter();
                    p.ParameterName = "@del";
                    p.Value = del;
                    cmd.Parameters.Add(p);
                    var x = cmd.ExecuteReader();

                    try
                    {
                        while (x.Read())
                        {
                            System_StafforRoleorDepartment d1 = new System_StafforRoleorDepartment();
                            d1.S_id = Convert.ToInt32(x["S_id"]);
                            d1.S_name = x["S_name"].ToString();
                            d1.S_phone = Convert.ToInt64(x["S_phone"]);
                            d1.S_sex = x["S_sex"].ToString();
                            d1.S_account = x["S_account"].ToString();
                            d1.S_password = x["S_password"].ToString();
                            d1.S_email = x["S_email"].ToString();
                            d1.S_IDcard = x["S_IDcard"].ToString();
                            d1.S_status = Convert.ToInt32(x["S_status"]);
                            d1.R_id = Convert.ToInt32(x["R_id"]);
                            d1.R_name = x["R_name"].ToString();
                            d1.S_del = Convert.ToInt32(x["S_del"]);
                            d1.S_time =DateTime.Parse(x["S_time"].ToString());
                            list.Add(d1);
                        }
                    }
                    finally
                    {
                        x.Close();
                    }
                }
                return list;
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="name">账号</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public int GetStaffLogin(string name, string pwd)
        {
            //string sql = "t_StLogin " + name + "," + pwd + "";
            //using (var cnn = DbConnection)
            //{
            //    using (var cmd = cnn.CreateCommand())
            //    {
            //        cmd.CommandText = sql;
            //        int i=Convert.ToInt32( cmd.ExecuteScalar());
            //        return i;
            //    }
            //}
            string sql = "t_StLogin";
            using (var cnn = DbConnection)
            {
                using (var cmd = cnn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = sql;
                    //cmd.CommandText = "t_StLogin"+" "+"'"+name+"'"+","+"'"+pwd+"'";
                    var p1 = cmd.CreateParameter();
                    p1.ParameterName = "@S_name";
                    p1.Value = name;
                    cmd.Parameters.Add(p1);

                    var p2 = cmd.CreateParameter();
                    p2.ParameterName = "@S_pwd";
                    p2.Value = pwd;
                    cmd.Parameters.Add(p2);
                    ////cmd.Parameters.Add("@S_name", SqlDbType.Text);
                    ////cmd.Parameters["@S_name"].value = name;

                    int i = Convert.ToInt32(cmd.ExecuteScalar());
                    return i;
                }
            }
        }


        public List<System_StaffState> StaffStateShow()
        {
            List<System_StaffState> list = new List<System_StaffState>();
            string str = string.Empty;
            str += "select t_Staff.S_id,t_Staff.S_name,t_Staff.S_account, t_Staff.S_phone,t_Staff.S_email,t_Staff.S_status,t_Staff.S_time,t_Staff.S_del from t_Staff where S_del=0";
            using (var cnn = DbConnection)
            {
                using (var cmd = cnn.CreateCommand())
                {
                    cmd.CommandText = str;  
                    var x = cmd.ExecuteReader();

                    try
                    {
                        while (x.Read())
                        {
                            System_StaffState d1 = new System_StaffState();
                            d1.S_id = Convert.ToInt32(x["S_id"]);
                            d1.S_name = x["S_name"].ToString();
                            d1.S_phone = Convert.ToInt64(x["S_phone"]);
                            d1.S_email = x["S_email"].ToString();
                            d1.S_status = Convert.ToInt32(x["S_status"]);
                            d1.S_account = x["S_account"].ToString();
                            d1.S_time = Convert.ToDateTime(x["S_time"]);
                            d1.S_StatusValue = EnumHelper.GetEnumSelectState<EnumStaffState>(d1.S_status);
                            list.Add(d1);
                        }
                    }
                    finally
                    {
                        x.Close();
                    }
                }
                return list;
            }
        }

    
    }
}
